<?php


class FrontendShopProductsModel extends Product
{
	public static function model($className=__CLASS__) {
		return parent::model($className);
	}
	public static function getDetail($productId)
	{
		$sql = "SELECT * FROM shop_products WHERE published=1 AND product_id=:pid";
		$command = Yii::app()->db->createCommand($sql);
		$command->bindParam(':pid', $productId);
		return $command->queryRow();
	}
	public static function getProductByCat($catId, $limit=30, $offset=0, $order="")
	{
		$where = ($catId==null)?"":" AND categoryId= ".intval($catId);
		$order = ($order!='')?$order:" ORDER BY id DESC ";
		$sql = "SELECT * FROM product WHERE status=1 $where $order LIMIT :offset, :limit ";
		$command = Yii::app()->db->createCommand($sql);
		$command->bindParam(':offset', $offset);
		$command->bindParam(':limit', $limit);
		return $command->queryAll();
	}	
	public static function getTotalProductByCat($catId)
	{
		$where = ($catId==null)?"":" AND categoryId= ".intval($catId);
		$sql = "SELECT count(*) as total FROM product WHERE status=1 $where ";
		$command = Yii::app()->db->createCommand($sql);
		$result = $command->queryRow();
		return $result['total'];
	}
	public static function getTotalProductPlus($catId)
	{
		$where = ($catId==null)?"":" AND masterId= ".intval($catId);
		$sql = "SELECT count(*) as total FROM product_plus WHERE plusType=2 $where ";
		$command = Yii::app()->db->createCommand($sql);
		$result = $command->queryRow();
		return $result['total'];
	}
	
	public function getTaxRate($variations = null, $amount = 1) {
		if($this->tax) {
			$taxrate = $this->tax->percent;
			$price = (float) $this->price;
			if($variations)
				foreach($variations as $key => $variation) {
				$price += @ProductVariation::model()->findByPk($variation[0])->price_adjustion;
			}
	
	
			(float) $price *= $amount;
	
			(float) $tax = $price * ($taxrate / 100);
	
			return $tax;
		}
	}
	public function getPrice($variations = null, $amount = 1) {
		$price = (float) $this->price;
		if($variations)
			foreach($variations as $key => $variation) {
			$price += @ProductVariation::model()->findByPk($variation[0])->price_adjustion;
		}
	
	
		(float) $price *= $amount;
	
		return $price;
	}
}